﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Windows.Threading;
using RateMyApp.Controls;
using RateMyApp;

using Microsoft.Phone.Tasks;
using AMobiSDK;

namespace Sai_Se_Dung
{
    public partial class Win : PhoneApplicationPage
    {
        public static DispatcherTimer m_timer;

        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            PlayBackgroundSound();
            m_timer.Stop();
            m_timer.Start();
            txtScoreWin.Text = Play.m_curScore.ToString();

            AdManager.setWidgetCode("f2257ad5570a4a773a2edd33504ef3c2");//Bạn phải thay mã này bằng widgetCode của bạn
            AdManager advertisement = new AdManager();
            advertisement.OnNavigationTo(this, null, null, banner);
        }

        private void PlayBackgroundSound()
        {
            if (Option.isOn == true)
            {
                mp3_win.AutoPlay = true;
                mp3_win.Play();
            }
            else
            {
                mp3_win.AutoPlay = false;
                mp3_win.Stop();
            }
        }

        public Win()
        {
            InitializeComponent();

            m_timer = new DispatcherTimer()
            {
                Interval = new TimeSpan(0, 0, 0, 0, 50)
            };

            m_timer.Tick += timer_Tick;

        }

        private void timer_Tick(object sender, EventArgs e)
        {
            Play.m_curTime--;

            if (Play.m_curTime >= 0)
            {
                Play.m_curScore += 100;
                txtScoreWin.Text = Play.m_curScore.ToString();
            }
            else 
            {
                // Save best score (if any)
                if (Play.m_curScore > new Score().ReadScore())
                {
                    new Score().WriteScore(Play.m_curScore.ToString());
                }
            }
        }

        private void PlayButtonSound()
        {
            if (Option.isOn == true)
            {
                mp3_btn.Play();
            }
        }

        private void btnPlayAgain_Click(object sender, RoutedEventArgs e)
        {
            PlayButtonSound();

            new Play().Reset();

            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            NavigationService.Navigate(new Uri("/Play.xaml", UriKind.Relative));
        }

        private void btnShareScore_Click(object sender, RoutedEventArgs e)
        {
            PlayButtonSound();

            ShareLinkTask shareLinkTask = new ShareLinkTask();
            shareLinkTask.Title = "FALSE is TRUE";
            shareLinkTask.LinkUri = new Uri("http://www.windowsphone.com/s?appid=38605eec-1bc6-49a3-8a5e-53e88555da9d", UriKind.Absolute);//cho linh cua minh vao
            shareLinkTask.Message = "I've got " + Play.m_curScore.ToString() + " scores!";
            shareLinkTask.Show();
        }

        private void mp3_win_MediaEnded(object sender, RoutedEventArgs e)
        {
            PlayBackgroundSound();
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (Option.isOn == true)
            {
                mp3_btn.Play();
            }

            new Play().Reset(); // Fix performance
            
            if (NavigationService.CanGoBack)
            {
                while (NavigationService.RemoveBackEntry() != null)
                {
                    NavigationService.RemoveBackEntry();
                }
            }
            NavigationService.Navigate(new Uri("/Confirm.xaml", UriKind.Relative));
        }

    }
}